import jsonp from 'jsonp'
import {message} from 'antd';
import ajax from './ajax';
import {PAGE_SIZE} from "../utils/constants";

/*
包含应用中所有接口请求函数的模块
每个函数的返回值都是Promise

基本要求：能根据接口文档定义接口请求函数
 */
const BASE = ''


/**
 * api请求接口函数
 */

/*
登录注册验证码
 */
//1、请求短信验证码
export const reqCodes = (mobile) => ajax(BASE + '/api/codes/',{mobile},"POST")
//2、请求注册
export const reqRegister=(username,code,password)=>ajax(BASE + '/api/register/',{username,code,password},"POST")
//3、请求登录
export const reqLogin=(username,password)=>ajax(BASE + '/api/login/',{username,password},"POST")
//4、忘记密码
export const reqForgetPassword=(username,code,password)=>ajax(BASE + '/api/forget/',{username,code,password},"POST")

/*
公司信息
 */
//1、请求公司信息
export const reqCompanyInformation = () => ajax(BASE + '/api/company/',{},"GET")
//2、创建公司信息
export const reqCreateCompanyInformation = (name,describe,address,contact,contact_phone) => ajax(BASE + '/api/company/',{name,describe,address,contact,contact_phone},"POST")
//3、修改公司信息
export const reqUpdateCompanyInformation = (companyId,name,describe,address,contact,contact_phone) => ajax(BASE + '/api/company/'+companyId+'/',{companyId,name,describe,address,contact,contact_phone},"PUT")

/*
充值信息
 */
//1、创建充值
export const reqRecharge = (amout) => ajax(BASE + '/api/recharge/',{amout},"POST")


/*
简历信息
 */
//1、公有库
export const reqPublicResume = (keyword,page, PAGE_SIZE) => ajax(BASE + '/api/public_resume/?search='+keyword,{keyword,page, PAGE_SIZE},"GET")
//2、私有库
export const reqPrivateResume = (page,PAGE_SIZE) => ajax(BASE + '/api/private_resume/',{page,PAGE_SIZE},"GET")
//3、公有库简历加入私有库消耗积分
export const reqCostPoint = (points,state) => ajax(BASE + '/api/cost_point/',{points,state},"POST")
//4、公有库简历加入私有库
export const reqPublicToPrivate=(employee)=> ajax(BASE + '/api/own/',{employee},"POST")
//5、判断公有库中简历情况和用户积分状态
export const reqIsOK=(employee,point)=> ajax(BASE + '/api/isok/',{employee,point},"GET")
//6、公有库私有库简历搜索
export const reqSearchResume = (value) => ajax(BASE + '/api/public_resume/?search='+value,{value},"GET")
//7、批量
export const reqIsAllOK=(employees)=> ajax(BASE+'/api/batch/',{employees},"POST")

/*
json请求的接口请求函数
 */
export const reqWeather = (city) => {
    return new Promise((resolve, reject) => {
        const url = `https://api.map.baidu.com/telematics/v3/weather?location=${city}&output=json&ak=3p49MVra6urFRGOT9s8UBWr2`
        //发送jsonp请求
        jsonp(url, {}, (err, data) => {
            //如果成功了
            if (!err && data.status === 'success') {
                //取出需要的数据
                const {dayPictureUrl, weather} = data.results[0].weather_data[0]
                resolve({dayPictureUrl, weather})
            } else {
                //如果失败了
                message.error('获取天气信息失败')
            }
        })

    })
}
